package cn.com.yiqi.wsc.portal.controller.appParam;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.appparam.entity.AppParamEntity;
import cn.com.yiqi.wsc.portal.controller.BaseController;
import cn.com.yiqi.wsc.service.sysparams.AppParamService;
import cn.com.yiqi.wsc.service.sysparams.dataobject.AppParamDO;
import cn.com.yiqi.wsc.service.sysparams.dataobject.DistributIonDO;
import cn.com.yiqi.wsc.service.sysparams.dataobject.PointGetRuleDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 杨贵森
 * @ClassName:AppParamController
 * @Title: AppParamController
 * @Description: TODO
 * @date 2017/3/15
 */
@Controller
@RequestMapping("/appParam")
public class AppParamController extends BaseController {
    @Autowired
    private AppParamService appParamService;


    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("param/index");
        this.getUserData(mv);
        return mv;
    }


    @RequestMapping("/appParamsList")
    @ResponseBody
    public Result getSYSAppParam(@RequestBody AppParamEntity appParamEntity){
        return   appParamService.getMarryAppParamList(appParamEntity);
    }

    @RequestMapping("/distributionParam")
    @ResponseBody
    public Result getDistributionAppParam(@RequestBody AppParamEntity appParamEntity){
        return   appParamService.getDistributionParamList(appParamEntity);
    }

    @RequestMapping("/updateAppParam")
    @ResponseBody
    public Result updateAppParam(@RequestBody AppParamDO appParamDO){
        Result result = new Result();

        if(appParamDO.getType().equals("SYS")){
            if(  StringUtils.isEmpty(appParamDO.getSysQQ())){
                return ResultUtil.paramError(result,"QQ号不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getSysQQ())){
                return ResultUtil.paramError(result,"标题不能为空！");
            }
        }

        if(appParamDO.getType().equals("POINT")){
            if(  StringUtils.isEmpty(appParamDO.getPointTotalTime())){
                return ResultUtil.paramError(result,"兑换次数不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getPointTotalMax())){
                return ResultUtil.paramError(result,"兑换总额度不能为空！");
            }

            if(  StringUtils.isEmpty(appParamDO.getPointMin())){
                return ResultUtil.paramError(result,"单次兑换最小不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getPointMax())){
                return ResultUtil.paramError(result,"单次兑换最大不能为空！");
            }

            if(  StringUtils.isEmpty(appParamDO.getPointExtraMin())){
                return ResultUtil.paramError(result,"额外兑换最小不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getPointExtraMax())){
                return ResultUtil.paramError(result,"额外兑换最大不能为空！");
            }
        }

        if(appParamDO.getType().equals("BALANCE")){
            if(  StringUtils.isEmpty(appParamDO.getBalanceEftchTimes())){
                return ResultUtil.paramError(result,"提现次数不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getBalanceEftchMin())){
                return ResultUtil.paramError(result,"提现额度最小不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getBalanceEftchMax())){
                return ResultUtil.paramError(result,"提现额度最大不能为空！");
            }
        }

        if(appParamDO.getType().equals("SIGNED")){
            if(CollectionUtils.isEmpty(appParamDO.getRules()) ){
                return ResultUtil.paramError(result,"签到规则不能为空！");
            }
        }

        if(appParamDO.getType().equals("BIRTH")){
            if(  StringUtils.isEmpty(appParamDO.getBirthdayAdvanceDays())){
                return ResultUtil.paramError(result,"生日提前天数不能为空！");
            }
            if(  StringUtils.isEmpty(appParamDO.getBirthdayRemindTime())){
                return ResultUtil.paramError(result,"生日提醒时间不能为空！");
            }

        }
        return   appParamService.updateAppParam(appParamDO);
    }

    @RequestMapping("/updateDistributionAppParam")
    @ResponseBody
    public Result  updateDistributionAppParam(@RequestBody List<DistributIonDO> distributionList){
        Result result = new Result();

        if(CollectionUtils.isEmpty(distributionList) ){
            return ResultUtil.paramError(result,"分销规则不能为空！");
        }
        Double rate=0.0;
        for(DistributIonDO distributionDo : distributionList){
            rate+=distributionDo.getRate();
        }
        if(rate>100 || rate<=0){
            return ResultUtil.paramError(result,"总分销比例应为0~100%！");
        }
        return  appParamService.updateDistributionAppParam(distributionList);
    }

    @RequestMapping("/updatePointGetRule")
    @ResponseBody
    public Result updatePointGetRule(@RequestBody PointGetRuleDO pointGetRuleDO) {
        Result result = new Result();
        result = appParamService.updatePointGetRule(pointGetRuleDO);
        return result;
    }

}
